﻿/*
 *动态验证表单数据
 * 刘院克 2010-4-12
*/
var css="show_input";
//判断是否为空
function checkNull(obj,spanName){
	var span=document.getElementById(spanName);
     if ((obj.value.length==0)||(obj.value==" ")){
	  span.innerHTML="不可为空";
	  //obj.className="hh_form_text";
	   return false;
	 }else{
		 //obj.className="hh_form_text";
		 span.innerHTML="";
		 }
		 return true;
  }
//计算两列数字大小
function checkdaxiao(obj2,obj1,spanName){
	var span=document.getElementById(spanName);
	 if(obj1.value.length==0){
		span.innerHTML="不可为空";
	    return false; 
	 }
     else if(isNaN(obj1.value)){
	   span.innerHTML="请填写数字";
	   return false;
	 }else{
		if(parseInt(obj1.value)>parseInt(obj2.value)){
			span.innerHTML="付款金额必须小于剩余款项";
 			return false;
		}else{
			span.innerHTML="";
		}
		return true;
	 }


}

//计算两列数字大小，主要用于年度计划新建中”可用预算金额“和“项目金额”的比较  HEIJJ 20140117
function checkdaxiao2(obj2,obj1,spanName){
	var span=document.getElementById(spanName);
	 if(obj1.value.length==0){
		span.innerHTML="不可为空";
	    return false; 
	 }
     else if(isNaN(obj1.value)){
	   span.innerHTML="请填写数字";
	   return false;
	 }else{
		if(parseInt(obj1.value)>parseInt(document.getElementById(obj2).value)){
		    var obj5=document.getElementsByName("col20");
		    var jihuastat="";
		    for(var i=0;i<obj5.length;i++)
		    {
		       if(obj5[i].checked){
		         var jihuastat=obj5[i].value;
		       }
		    }
			 
			 var flag=false;
		      if (jihuastat=="计划外项目")
		      {
		        flag=true;
		        var s=obj1.value;
		s=s.replace(/^(\d*)$/,"$1.");
        s=(s+"00").replace(/(\d*\.\d\d)\d*/,"$1");
        var re=/(\d)(\d{3},)/;
        while(re.test(s))
        s=s.replace(re,"$1,$2");
        s=s.replace(/,(\d\d)$/,".$1");
        obj1.value= s.replace(/^\./,"0.");
		        span.innerHTML="";
		      }
		      else
		      {
		        span.innerHTML="项目金额必须小于可用预算金额,否则请选择为“计划外”";
		        flag=false;
		      }
			
 			return flag;
		}else{
			span.innerHTML="";
			var s=obj1.value;
		s=s.replace(/^(\d*)$/,"$1.");
        s=(s+"00").replace(/(\d*\.\d\d)\d*/,"$1");
        var re=/(\d)(\d{3},)/;
        while(re.test(s))
        s=s.replace(re,"$1,$2");
        s=s.replace(/,(\d\d)$/,".$1");
        obj1.value= s.replace(/^\./,"0.");
			return true;
		}
		
	 }


}
//是否为数字
function checkNumber(obj,spanName){
	var span=document.getElementById(spanName);
	 if(obj.value.length==0){
		span.innerHTML="不可为空";
	    //obj.className="hh_form_text";
	    return false; 
	 }
     else if(isNaN(obj.value)){
	   span.innerHTML="请填写数字";
	   //obj.className="hh_form_text";
	   return false;
	 }else{
		 //obj.className=css;
		 span.innerHTML="";
		 return true;
	 }
}

//计算
function calculate(obj1,obj2,obj3,sign){
	if(sign=="+"){
		obj3.value=obj1.value+obj2.value;
	}else if(sign=="-"){
		obj3.value=obj1.value-obj2.value;
	}else if(sign=="*"){
		obj3.value=obj1.value*obj2.value;	
	}else if(sign=="/"){
		obj3.value=obj1.value/obj2.value;
	}
	alert(obj3.value);
}
//日期判断
	function checkdate(obj,spanName) {
	var span=document.getElementById(spanName);
	  var re=/^\d{4}-\d{1,2}-\d{1,2}$/;
      if(!re.test(obj.value)) {
				 span.innerHTML="日期格式不正确！";
	            //obj.className="hh_form_text";
				return false;
            }else{
				 var list=obj.value.split("-");
				 var date=new Date(list[0],list[1]-1,list[2]);
					 if(date.getFullYear()==list[0] && (date.getMonth()+1)==list[1] && date.getDate()==list[2]){
							 //obj.className=css;
						    span.innerHTML="";
							return true;
							}else{
						span.innerHTML="日期格式不正确！";
						//obj.className="hh_form_text";
						return false;
						}
				}
    }
//电话号码合法检验
	  function checktel(obj,spanName){
	    var span=document.getElementById(spanName);
        var reg=/(^[0-9]{3,4}\-[0-9]{7,8}$)|(^[0-9]{7,8}$)|(^\([0-9]{3,4}\)[0-9]{3,8}$)|(^0{0,1}13[0-9]{9}$)|(^0{0,1}15[0-9]{9}$)|(^0{0,1}18[0-9]{9}$)/;
           if(!reg.test(obj.value)) {
                    span.innerHTML="电话号码格式不正确";
					//obj.className="hh_form_text";
					 return false;
					}else{
					   //obj.className=css;
					 span.innerHTML="";	
					 return true;
					}
            }
//身法证合法检验
function checkCard(obj,spanName){
  var aCity={11:"北京市",12:"天津市",13:"河北省",14:"山西省",15:"内蒙古自治区",21:"辽宁省",22:"吉林省",23:"黑龙江省",
	31:"上海市",32:"江苏省",33:"浙江省",34:"安徽省",35:"福建省",36:"江西省",37:"山东省",41:"河南省",42:"湖北省",43:"湖南省",
	44:"广东省",45:"广西壮族自治区",46:" 海南省",50:"重庆市",51:"四川省",52:"贵州省",53:"云南省",54:"西藏自治区",61:"陕西省",
	62:"甘肃省",63:"青海省",64:"宁夏回族自治区",65:"新疆维吾尔自治区",71:"台湾省",81:"香港特别行政区",82:"澳门特别行政区",
	91:"国外"};
	    var cardid=obj.value;
	    var span=document.getElementById(spanName);
	    var reg=/^\d{17}(\d|X)$/i;
	if(reg.test(cardid)){
		//cardid=cardid.replace(/X$/i,"a");//将最后的X转为a
		if(aCity[parseInt(cardid.substr(0,2))]!=null){
             //obj.className=css;
		    span.innerHTML="";
			return true;
			}else{
				span.innerHTML="证件号码格式不正确";
				 //obj.className="hh_form_text";
				 return false;
				}
		}else{
		 span.innerHTML="证件号码格式不正确";
		 //obj.className="hh_form_text";
		 return false;
		 }
}
//字符长度限制
     function checkstrlen(obj,spanName,min,max){
	    var span=document.getElementById(spanName);
		var len=0;
		var i;
		var str=obj.value;
		for(i=0;i<str.length;i++){
			if(str.charCodeAt(i)>255)
			len+=2;
			else
			 len++;
		}
		if(len>= parseInt(min) && len<= parseInt(max)){
			 //obj.className=css;
		    span.innerHTML="";
			return true;
			}else{
				 span.innerHTML="输入长度为"+min+"到"+max+"位";
				 //obj.className="hh_form_text";
				 return false;
			}
		}
//字符或数字
    function checkDataStr(obj,spanName){
	    var span=document.getElementById(spanName);
			var re=/^[0-9a-z][\w-.]*[0-9a-z]$/i;
			if(re.test(obj.value)){
			 //obj.className=css;
		    span.innerHTML="";
			return true;
			}
			else{
				 span.innerHTML="必须为字符或数字";
				 //obj.className="hh_form_text";
				 return false;
				}
		}
//电子邮件验证
function checkEmail(obj,spanName){
	    var span=document.getElementById(spanName);
		 var Expression=/\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/; 
		 var objExp=new RegExp(Expression);
		 if(objExp.test(obj.value)==true){
			 //obj.className=css;
		    span.innerHTML="";
		    return true;
		 }else{
			span.innerHTML="邮箱格式不正确";
		    //obj.className="hh_form_text";
		    return false;
		 }
}
//特殊字符
function checkStrangeStr(obj,spanName){
	var span=document.getElementById(spanName);
	var testStr=/<|>|'|&|#|\$|\*|\[|\]|\{|\}|\%|\`|\||\:|\\|\//;
	if(testStr.test(obj.value)){
			span.innerHTML="含有特殊字符！";
		    //obj.className="hh_form_text";
		    return false;
		}else{
		    //obj.className=css;
		    span.innerHTML="";
		    return true;
			}
	}
//IP地址的合法校验
function checkIP(obj,spanName)
{
	var span=document.getElementById(spanName);
    var reg=/^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/; 
    if(reg.test(obj.value)){
		 //obj.className=css;
		    span.innerHTML="";
		    return true;
		}else{
			span.innerHTML="IP地址格式不正确！";
		    //obj.className="hh_form_text";
		    return false;
			}
}
//数据中间空格的判断
//中间含有空格
function checkSpace(obj,spanName){
	var span=document.getElementById(spanName);
		var whitespace=" \t\n\r";
		var s=obj.value;
	    var i;
			for(i=0;i<s.length;i++){
				var c=s.charAt(i);
				if(whitespace.indexOf(c)>=0){
					span.innerHTML="字符中间含有空格！";
					//obj.className="hh_form_text";
					return false;
					}
				}
	            //obj.className=css;
		        span.innerHTML="";
		        return true;
	     }
//下拉列表选中否
function checkSelect(obj,spanName){
	 var span=document.getElementById(spanName);
	if(obj.selectedIndex==0){
		span.innerHTML="列表必须选择一项";
		return false;
		}else{
			span.innerHTML="";
			return true;
			}
	}
//限制上传附件的大小
function checkFileSize(obj,spanName,max){  
     var fso=new ActiveXObject("Scripting.FileSystemObject");
     var file=fso.GetFile(obj.value);
	var span=document.getElementById(spanName);
	if(file.size>parseInt(max)*1024*1024){
	span.innerHTML="文件不能超过"+max+"M";
	return false;
		}else{
			span.innerHTML="";
	         return true;
			}
	}
//清除数据
function clearSpan(obj,spanName){
    //obj.className=css;
	document.getElementById(spanName).innerHTML="&nbsp;";
	}
//checkbox最少选择一个
function checkBox(obj,spanName){
	var span=document.getElementById(spanName);
	var cbox=obj;
	var i=0;
    for(;i<cbox.length;i++){
		if(cbox[i].checked==true){
			span.innerHTML="";
			break;
			return true;
  			}
        }
		if(i==cbox.length){
				  span.innerHTML="至少选择一个";
                  return false;
				}
	}
	
	function checkNumberTwo(obj,spanName){
	var span=document.getElementById(spanName);
	 if(obj.value.length==0){
		span.innerHTML="不可为空";
	    return false; 
	 }
     else if(isNaN(obj.value)){
	   span.innerHTML="请填写数字";
	   return false;
	 }else{
		 span.innerHTML="";
		 var s=obj.value;
		s=s.replace(/^(\d*)$/,"$1.");
        s=(s+"00").replace(/(\d*\.\d\d)\d*/,"$1");
        var re=/(\d)(\d{3},)/;
        while(re.test(s))
        s=s.replace(re,"$1,$2");
        s=s.replace(/,(\d\d)$/,".$1");
        obj.value= s.replace(/^\./,"0.");
		 return true;
	 }
}